package leetcode.offer;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: LengthOfLongestSubstring
 * Description: leetcode-剑指offer 48.最长不含重复字符的子字符串
 * date: 2021-11-08 22:02
 *
 * @author liyifan
 */
public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int res = 0;
        int temp = 0;
        for (int j = 0;j < s.length();j++) {
            int i = map.getOrDefault(s.charAt(j), -1);
            map.put(s.charAt(j), j);
            temp = temp < j - i ? temp + 1 : j - i;
            res = Math.max(res, temp);
        }
        return res;
    }
}
